<?php 
/**
* Test adding existing and uploaded file.
*/
class DomArFileTestCase extends DomArTestUnitTestCase {

	public function setUp() {
    	parent::setUp();
    }
    
    public function tearDown() {
    	parent::tearDown();
    }

	public function testMisc() {
		
		$testFile = 'domarTestFile.txt';
		$dir = 'test/';
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.$testFile, 'test test test');
		
		$testFile2 = $dir . $testFile;
		mkdir(DOMAR_FILE_DEFAULT_PATH . $dir);
		file_put_contents( DOMAR_FILE_DEFAULT_PATH . $testFile2, 'test test test');
		
		$file = new DomArFile( $testFile, array('txt','doc'), DOMAR_FILE_DEFAULT_PATH, DOMAR_FILE_DEFAULT_URL );	
		
		$this->assertTrue( $file->isPathSafe('myFile') );
		
		// --------------------------------------------------------------
		// test isSafe methods
		$this->assertTrue( $file->isExtensionSafe('txt') );
		$this->assertFalse( $file->isExtensionSafe('ggg') );
		
		$this->assertTrue( $file->isFileNameSafe('myFile.txt') );
		$this->assertTrue( $file->isFileNameSafe('myFile76_,\'- aa') );
		$this->assertFalse( $file->isFileNameSafe('"') );
		$this->assertFalse( $file->isFileNameSafe('./') );
		$this->assertFalse( $file->isFileNameSafe('/') );
		$this->assertFalse( $file->isFileNameSafe('\\') );
		
		
		$this->assertTrue( $file->isPathSafe('/data/dir/myFile.txt') );
		$this->assertTrue( $file->isPathSafe('/data/dir/myFile') );
		$this->assertTrue( $file->isPathSafe('/data/dir/') );
		$this->assertTrue( $file->isPathSafe('myFile.txt') );
		
		$this->assertTrue( $file->isPathSafe('/') );
		$this->assertTrue( $file->isPathSafe('') );
		$this->assertFalse( $file->isPathSafe('/data/../myFile.txt') );
		$this->assertTrue( $file->isPathSafe('/data/./myFile.txt') );
		$this->assertTrue( $file->isPathSafe('dir/myDir76._,\'- aa/myFile.txt') );
		$this->assertTrue( $file->isPathSafe('dir/myDir76._,\'- aa/') );
		$this->assertFalse( $file->isPathSafe('/??/') );
		$this->assertFalse( $file->isPathSafe('/""/') );
		
		
		// --------------------------------------------------------------
		// test get methods
		$this->assertEqual( $file->getRoot(), DOMAR_FILE_DEFAULT_PATH);
		$this->assertEqual( $file->getRootUrl(), DOMAR_FILE_DEFAULT_URL);
		$this->assertEqual( $file->getPath(), DOMAR_FILE_DEFAULT_PATH . $testFile );
		$this->assertEqual( $file->getUrl(), DOMAR_FILE_DEFAULT_URL . $testFile );
		$this->assertEqual( $file->getExt(), 'txt');
		$this->assertEqual( $file->getName(), 'domarTestFile');
		$this->assertEqual( $file->getNameExt(), $testFile);
		$this->assertEqual( $file->getDir(), '');
		$this->assertEqual( $file->getRelativePath(), $testFile);
		
		// --------------------------------------------------------------
		// test get methods again
		
		$file = new DomArFile( $testFile2, array('txt','doc'), DOMAR_FILE_DEFAULT_PATH, DOMAR_FILE_DEFAULT_URL );
		$this->assertEqual( $file->getRoot(), DOMAR_FILE_DEFAULT_PATH);
		$this->assertEqual( $file->getRootUrl(), DOMAR_FILE_DEFAULT_URL);
		$this->assertEqual( $file->getPath(), DOMAR_FILE_DEFAULT_PATH . $testFile2 );
		$this->assertEqual( $file->getUrl(), DOMAR_FILE_DEFAULT_URL . $testFile2 );
		$this->assertEqual( $file->getExt(), 'txt');
		$this->assertEqual( $file->getName(), 'domarTestFile');
		$this->assertEqual( $file->getNameExt(), $testFile);
		$this->assertEqual( $file->getDir(), $dir );
		$this->assertEqual( $file->getRelativePath(), $testFile2);

		
		
		
		
		unlink(DOMAR_FILE_DEFAULT_PATH.$testFile);
		unlink(DOMAR_FILE_DEFAULT_PATH.$testFile2);
		rmdir(DOMAR_FILE_DEFAULT_PATH . $dir);
	}
	
	public function testMakeTmpFileName() {
	
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.'domarTestFile.txt', 'test test test');
	
		$file = new DomArFile( 'domarTestFile.txt', array('txt','doc'), DOMAR_FILE_DEFAULT_PATH, DOMAR_FILE_DEFAULT_URL );	
		$copy1 = $file->copy();
		$copy2 = $file->copy();
		$copy3 = $copy1->copy();
		
		$this->assertEqual( $copy1->getParent(), NULL);
		$this->assertEqual( $copy1->getProperty(), NULL);
		
		$this->assertEqual( $copy2->getParent(), NULL);
		$this->assertEqual( $copy2->getProperty(), NULL);
		
		$this->assertEqual( $copy3->getParent(), NULL);
		$this->assertEqual( $copy3->getProperty(), NULL);
		
		$this->assertEqual( $file->getRelativePath(), 'domarTestFile.txt' );
		$this->assertEqual( $copy1->getRelativePath(), 'domarTestFile(1).txt' );
		$this->assertEqual( $copy2->getRelativePath(), 'domarTestFile(2).txt' );
		$this->assertEqual( $copy3->getRelativePath(), 'domarTestFile(1)(1).txt' );
		
		$this->assertTrue( $file->exists() );
		$this->assertTrue( $copy1->exists() );
		$this->assertTrue( $copy2->exists() );
		$this->assertTrue( $copy3->exists() );
		
		$file->unlink();
		$copy1->unlink();
		$copy2->unlink();
		$copy3->unlink();
	}
	
	public function testChangeParent() {
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.'a.txt', 'test test test');
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.'b.txt', 'test test test');
	
		$t = new DomArTestClass_DomArFile('t'); 
		$this->assertEqual( $a->file, '');
	
		$a = new DomArTestClass_DomArFile('a');
		$a->forceSet('file','a.txt');
		$b = new DomArTestClass_DomArFile('b');
		$b->forceSet('file','b.txt');
		
		$this->assertEqual( get_class($a->file), 'DomArFile');
		$this->assertEqual( get_class($b->file), 'DomArFile');
		$this->assertTrue( $a->file->exists() );
		$this->assertTrue( $b->file->exists() );
	
		$bFile = $b->file;

		try {
			$b->file = $a->file;
			$this->assertTrue(false);
		} catch( Exception $e ) {
			$this->assertTrue(true);
		}
		
		$b->file = clone $a->file;
		
		$this->assertEqual( get_class($a->file), 'DomArFile');
		$this->assertEqual( $a->file->getRelativePath(), 'a.txt');
		$this->assertTrue( $a->file->exists() );

		$this->assertFalse( $bFile->exists() );
		
		$this->assertEqual( $b->file->getRelativePath(), 'a(1).txt');
		$this->assertTrue( $b->file->exists() );

		$b->file->setAllowedExtensions(array('txt'));
		$this->assertNotEqual( $a->file->getAllowedExtensions(), $b->file->getAllowedExtensions() );

		$bFile->unlink();
		$a->file->unlink();
		$b->file->unlink();
	}
	
	public function testCreateDirTree() {
		$file = new DomArFile( 'domarTestFile.txt', array('txt','doc'), DOMAR_FILE_DEFAULT_PATH, DOMAR_FILE_DEFAULT_URL );	
		
		$this->assertFalse( is_dir(DOMAR_FILE_DEFAULT_PATH.'test1/') );
		$this->assertFalse( is_dir(DOMAR_FILE_DEFAULT_PATH.'test1/test2/test3/') );
		
		$file->createDirectoryTree('test1/');
		$this->assertTrue( is_dir(DOMAR_FILE_DEFAULT_PATH.'test1/') );
		$file->createDirectoryTree('test1/test2/test3/');
		$this->assertTrue( is_dir(DOMAR_FILE_DEFAULT_PATH.'test1/test2/test3/') );

		rmdir( DOMAR_FILE_DEFAULT_PATH.'test1/test2/test3' );
		rmdir( DOMAR_FILE_DEFAULT_PATH.'test1/test2' );
		rmdir( DOMAR_FILE_DEFAULT_PATH.'test1' );
	}

	public function testRename() {
		
		file_put_contents( DOMAR_FILE_DEFAULT_PATH . 'domarTestFile.txt', 'test test test');
		$this->assertTrue( is_file(DOMAR_FILE_DEFAULT_PATH.'domarTestFile.txt') );
		if( is_file(DOMAR_FILE_DEFAULT_PATH.'mydir/domarRenamedTestFile.txt') )
			unlink( DOMAR_FILE_DEFAULT_PATH . 'mydir/domarRenamedTestFile.txt');
		if( is_dir(DOMAR_FILE_DEFAULT_PATH.'mydir/') )
			rmdir( DOMAR_FILE_DEFAULT_PATH . 'mydir');
		
		$file = new DomArFile( 'domarTestFile.txt', array('txt','doc'), DOMAR_FILE_DEFAULT_PATH, DOMAR_FILE_DEFAULT_URL );
		
		$file->rename( 'my2dir/' );
		$this->assertTrue( is_file(DOMAR_FILE_DEFAULT_PATH.'my2dir/domarTestFile.txt') );
		
		$file->rename( 'domarRenamedTestFile3' );
		$this->assertTrue( is_file(DOMAR_FILE_DEFAULT_PATH.'my2dir/domarRenamedTestFile3.txt') );
		
		$file->rename( '/domarRenamedTestFile3' );
		$this->assertTrue( is_file(DOMAR_FILE_DEFAULT_PATH.'domarRenamedTestFile3.txt') );
		
		$file->rename( 'my3dir/' );
		$this->assertTrue( is_file(DOMAR_FILE_DEFAULT_PATH.'my3dir/domarRenamedTestFile3.txt') );
		
		$file->rename( '\domarRenamedTestFile3' );
		$this->assertTrue( is_file(DOMAR_FILE_DEFAULT_PATH.'domarRenamedTestFile3.txt') );
		
		$file->rename( 'mydir/' );
		$this->assertTrue( is_file(DOMAR_FILE_DEFAULT_PATH.'mydir/domarRenamedTestFile3.txt') );
		
		$file->rename( 'mydir/domarRenamedTestFile2' );
		$this->assertTrue( is_file(DOMAR_FILE_DEFAULT_PATH.'mydir/domarRenamedTestFile2.txt') );
		
		$file->rename( 'mydir/domarRenamedTestFile2.doc' );
		$this->assertTrue( is_file(DOMAR_FILE_DEFAULT_PATH.'mydir/domarRenamedTestFile2.doc') );
		
		$file->rename( 'mydir/domarRenamedTestFile.txt' );
		$this->assertTrue( is_file(DOMAR_FILE_DEFAULT_PATH.'mydir/domarRenamedTestFile.txt') );
		
		try { $file->rename( '"' ); $this->assertTrue( false ); } catch( InvalidArgumentException $e ) { $this->assertTrue( true); }
		try { $file->rename( '?' ); $this->assertTrue( false ); } catch( InvalidArgumentException $e ) { $this->assertTrue( true); }
		try { $file->rename( '|' ); $this->assertTrue( false ); } catch( InvalidArgumentException $e ) { $this->assertTrue( true); }
		
		
		$file->rename( 'mySecDir/' );
		$this->assertTrue( !is_file(DOMAR_FILE_DEFAULT_PATH.'mydir/domarRenamedTestFile.txt') );
		$this->assertTrue( is_file(DOMAR_FILE_DEFAULT_PATH.'mySecDir/domarRenamedTestFile.txt') );
		
		try { $file->rename( '/' ); $this->assertTrue( true ); } catch( InvalidArgumentException $e ) { $this->assertTrue( false ); }
		try { $file->rename( '\\' ); $this->assertTrue( true ); } catch( InvalidArgumentException $e ) { $this->assertTrue( false ); }
		
		$file->unlink();
		$this->assertFalse( is_dir(DOMAR_FILE_DEFAULT_PATH.'mydir') );
		$this->assertFalse( is_dir(DOMAR_FILE_DEFAULT_PATH.'my2dir/') );
		$this->assertFalse( is_file(DOMAR_FILE_DEFAULT_PATH.'domarRenamedTestFile.txt') );
	}
	
	public function testAutoRename() {
		DomArTestClass_DomArFile::getTable()->update();
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.'myFile.txt', 'test test test');
		
		
		$file = new DomArTestClass_DomArFile('myFile');
		$file->autoFile->addExisting(DOMAR_FILE_DEFAULT_PATH.'myFile.txt', 'm');
		$this->assertTrue( $file->autoFile->exists() );
		$this->assertEqual( $file->autoFile->getRelativePath(), 'm.txt');
		
		$this->assertTrue($file->save());
		$this->assertObjectExist( $file );
		$this->assertObjectDatabaseValue( $file, 'autoFile', 'DomArTestClass_DomArFile/1-autoFile.txt');
		$this->assertEqual( $file->autoFile->getRelativePath(), 'DomArTestClass_DomArFile/1-autoFile.txt');
		
		
		$file->autoFile->delete();
		unlink(DOMAR_FILE_DEFAULT_PATH.'myFile.txt');
	}
	
	public function testAutoRenameWithRelative() {
		DomArTestClass_DomArFile::getTable()->update();
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.'myFile.txt', 'test test test');
		
		
		$file = new DomArTestClass_DomArFile('myFile');
		$file->autoFileWithRelative->addExisting(DOMAR_FILE_DEFAULT_PATH.'myFile.txt', 'm');
		$this->assertTrue( $file->autoFileWithRelative->exists() );
		$this->assertEqual( $file->autoFileWithRelative->getRelativePath(), 'm.txt');
		
		$this->assertTrue($file->save());
		$this->assertObjectExist( $file );

		$this->assertObjectDatabaseValue( $file, 'autoFileWithRelative', 'DomArTestClass_DomArFile/1-autoFileWithRelative.txt');
		$this->assertEqual( $file->autoFileWithRelative->getRelativePath(), 'DomArTestClass_DomArFile/1-autoFileWithRelative.txt');
		
		
		$file->autoFile->delete();
		unlink(DOMAR_FILE_DEFAULT_PATH.'myFile.txt');
	}
	
	public function testAutoRenameTree() {
		DomArTestClass_DomArFile::getTable()->update();
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.'myFile.txt', 'test test test');
		
		
		$file = new DomArTestClass_DomArFile('myFile');
		$file->autoFileTree->addExisting(DOMAR_FILE_DEFAULT_PATH.'myFile.txt', 'm');
		$this->assertTrue( $file->autoFileTree->exists() );
		$this->assertEqual( $file->autoFileTree->getRelativePath(), 'm.txt');
		
		$this->assertTrue($file->save());
		$this->assertObjectExist( $file );
		$this->assertObjectDatabaseValue( $file, 'autoFileTree', 'DomArTestClass_DomArFile/0000000/0000/1/autoFileTree.txt');
		$this->assertEqual( $file->autoFileTree->getRelativePath(), 'DomArTestClass_DomArFile/0000000/0000/1/autoFileTree.txt');
		
		
		$file->autoFileTree->delete();
		unlink(DOMAR_FILE_DEFAULT_PATH.'myFile.txt');
	}
	
	public function testAutoRenameIdTreeFunction() {
		
		$this->assertEqual( DomArFile::makeIdTree(0), '0000000/0000');
		$this->assertEqual( DomArFile::makeIdTree(10), '0000000/0000');
		$this->assertEqual( DomArFile::makeIdTree(100), '0000000/0000');
		$this->assertEqual( DomArFile::makeIdTree(1000), '0000000/0001');
		$this->assertEqual( DomArFile::makeIdTree(10000), '0000000/0010');
		$this->assertEqual( DomArFile::makeIdTree(100000), '0000000/0100');
		$this->assertEqual( DomArFile::makeIdTree(1000000), '0000001/0000');
		$this->assertEqual( DomArFile::makeIdTree(1001000), '0000001/0001');
		$this->assertEqual( DomArFile::makeIdTree(1021000), '0000001/0021');
		
		$this->assertEqual( DomArFile::makeIdTree(127543212), '0000127/0543');
		$this->assertEqual( DomArFile::makeIdTree(1546576752), '0001546/0576');

	}
	
	public function testAutoRenameMethod() {
		DomArTestClass_DomArFile::getTable()->update();
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.'myFile.txt', 'test test test');
		
		
		$file = new DomArTestClass_DomArFile('myFile');
		$file->autoFileMethod->addExisting(DOMAR_FILE_DEFAULT_PATH.'myFile.txt', 'm');
		$this->assertTrue( $file->autoFileMethod->exists() );
		$this->assertEqual( $file->autoFileMethod->getRelativePath(), 'm.txt');
		
		$this->assertTrue($file->save());
		$this->assertObjectExist( $file );
		$this->assertObjectDatabaseValue( $file, 'autoFileMethod', 'dir/DomArTestClass_DomArFile/1/autoFileMethod.txt');
		$this->assertEqual( $file->autoFileMethod->getRelativePath(), 'dir/DomArTestClass_DomArFile/1/autoFileMethod.txt');
		
		
		$file->autoFileTree->delete();
		unlink(DOMAR_FILE_DEFAULT_PATH.'myFile.txt');
	}
	
	public function testSetRoot() {
		$testFile = 'domarTestFile.txt';
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.$testFile, 'test test test');
		$newRoot = DOMAR_FILE_DEFAULT_PATH.'newRoot/';
		mkdir( $newRoot );
		
		$file = new DomArFile( $testFile, array('txt','doc'), DOMAR_FILE_DEFAULT_PATH, DOMAR_FILE_DEFAULT_URL );
		$file->setRoot( $newRoot );
		$this->assertTrue( file_exists($newRoot . $testFile) );
		$this->assertEqual( $file->getRelativePath(), $testFile);
		
		unlink($newRoot.$testFile);
		rmdir( $newRoot );
	}
	
	public function testAddExistingFile() {
		$testFile = 'domarTestFile.txt';
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.$testFile, 'test test test');
		$testFile2 = 'domarTestFile2.txt';
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.$testFile2, 'test test test');


		$file = new DomArFile( $testFile, array('txt','doc'), DOMAR_FILE_DEFAULT_PATH, DOMAR_FILE_DEFAULT_URL );
		
		$this->assertTrue($file->addExisting( DOMAR_FILE_DEFAULT_PATH.$testFile2, 'myDir/myFile'));
		
		$this->assertFalse( file_exists(DOMAR_FILE_DEFAULT_PATH.$testFile) );
		$this->assertTrue( file_exists(DOMAR_FILE_DEFAULT_PATH.$testFile2) );
		$this->assertTrue( file_exists(DOMAR_FILE_DEFAULT_PATH.'myDir/myFile.txt') );
		
		unlink( DOMAR_FILE_DEFAULT_PATH.$testFile2 );
		$file->unlink();
		
		
	}
	
	public function testUnlink() {
		mkdir(DOMAR_FILE_DEFAULT_PATH.'myDir');
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.'myDir/myFile.txt', 'test test test');
		

		$file = new DomArFile( 'myDir/myFile.txt', array('txt') );
		$this->assertTrue( file_exists(DOMAR_FILE_DEFAULT_PATH.'myDir/myFile.txt') );
		$this->assertTrue( is_dir(DOMAR_FILE_DEFAULT_PATH.'myDir/') );
		
		$file->unlink();
		
		$this->assertFalse( file_exists(DOMAR_FILE_DEFAULT_PATH.'myDir/myFile.txt') );
		$this->assertFalse( is_dir(DOMAR_FILE_DEFAULT_PATH.'myDir/') );
		
		
	}
	
	public function testSave() {
		DomArTestClass_DomArFile::getTable()->update();
		
		
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.'myFile.txt', 'test test test');
		$file = new DomArTestClass_DomArFile('myFile');
		$file->forceSet('file', 'myFile.txt');
		$this->assertTrue( $file->file->exists() );
		
		$file->save();
		$this->assertObjectExist( $file );
		$this->assertObjectDatabaseValue( $file, 'name', 'myFile' );
		$this->assertObjectDatabaseValue( $file, 'file', 'myFile.txt');
		
		$file->delete();
		$this->assertObjectNotExist( $file );
		$this->assertTableEmpty( $file->getTableName() );
		
		$file->file->unlink();
		$this->assertFalse( file_exists( DOMAR_FILE_DEFAULT_PATH.'myFile.txt') );
		
	
	}
	
	
	public function testListeners() {
		DomArTestClass_DomArFile::getTable()->update();
		
		
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.'myFile.txt', 'test test test');
		
		$file = new DomArTestClass_DomArFile('');
		$file->file->addExisting( DOMAR_FILE_DEFAULT_PATH.'myFile.txt' );
		
		$this->assertTrue( $file->file->exists() );
		
		$this->assertTrue( $file->beforeAdd );
		$this->assertTrue( $file->afterAdd );

		$file->file->unlink();
		
		$this->assertFalse( file_exists( DOMAR_FILE_DEFAULT_PATH.'myFile(1).txt') );
		unlink(DOMAR_FILE_DEFAULT_PATH.'myFile.txt');
		
	
	}
	
	public function testRepairName() {
		$this->assertEqual(DomArFile::repairName('aaaa'),'aaaa');
		$this->assertEqual(DomArFile::repairName('aöäüõÖÄÜÕ'),'aoauoOAUO');
		$this->assertEqual(DomArFile::repairName('""'),'');
		$this->assertEqual(DomArFile::repairName('ee.hpg'),'ee.hpg');
	}

	public function testValidation() {
		
		DomArTestClass_DomArFile::getTable()->update();
		
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.'myFile.txt', 'test test test');
		file_put_contents( DOMAR_FILE_DEFAULT_PATH.'myFile.asdf', 'test test test');
		
		$file = new DomArTestClass_DomArFile('myFile');
		$file->file->addExisting( DOMAR_FILE_DEFAULT_PATH.'myFile.asdf' );
		$this->assertFalse( $file->validated() );
		$file->setValidated(true);
		
		$file->file->addExisting( DOMAR_FILE_DEFAULT_PATH.'myFile.txt' );
		$this->assertTrue( $file->validated() );
		
		$file->file->unlink();
		
		unlink( DOMAR_FILE_DEFAULT_PATH.'myFile.txt' );
		$this->assertFalse( file_exists( DOMAR_FILE_DEFAULT_PATH.'myFile.txt') );
		unlink( DOMAR_FILE_DEFAULT_PATH.'myFile.asdf' );
		$this->assertFalse( file_exists( DOMAR_FILE_DEFAULT_PATH.'myFile.asdf') );
	}
}
?>